﻿/*------------- FingerPictureBox.cs
* Copyright (C): 2015  Supper Game
* Author       : 张铭林
* Version      : V1.01
* Date         : 2015/04/01 14:45:15
*
*/
/***************************************************************
* 
***************************************************************/

/*************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace zcode
{
    /// <summary>
    /// 方便手指操作的用户控件，轻松完成图像拖入和删除
    /// 主要属性：
    ///     ImageLayout：设置图像布局（None、Tile、Center、Stretch、Zoom）
    ///     ImageSize：控件大小
    ///     InitialImage：初始图像
    ///     ActiveImage：活动图像
    /// </summary>
    public partial class FramePictureBox : UserControl
    {
        private Int32 mouseX;
        private Int32 mouseY;
        private Image _InitialImage = null;
        private Image _FrameImage = null;
        private Image _Picture = null;
        private Rectangle _PictureRegion = new Rectangle(0, 0, 0, 0);

        public FramePictureBox()
        {
            InitializeComponent();

            // 设置用户控件属性，允许用户在上面拖放数据
            this.AllowDrop = true;

            // 设置装载事件处理器
            this.Load += new EventHandler(this.FingerPictureBox_Load);

            // 设置拖动事件处理器
            this.DragDrop += new DragEventHandler(this.FingerPictureBox_DragDrop);
            this.DragEnter += new DragEventHandler(this.FingerPictureBox_DragEnter);
        }

        private void FingerPictureBox_Load(object sender, EventArgs e)
        {
            // 设置图片框停靠位置和方式
            this.Controls[0].Dock = DockStyle.None;

            // 设定图片框背景图像布局
            this.Controls[0].BackgroundImageLayout = ImageLayout.Stretch;

            // 设置图片框的起始位置和大小
            this.Controls[0].Location = new Point(0, 0);
            this.Controls[0].Size = this.Size;

            // 设置背景图像改变事件处理器
            this.Controls[0].BackgroundImageChanged += new EventHandler(this.FingerPictureBox_BackgroundImageChanged);
        }

        private void FingerPictureBox_DragDrop(object sender, DragEventArgs e)
        {   //  获取拖入的文件
            String[] DropFiles = (String[])(e.Data.GetData(DataFormats.FileDrop));
            if (DropFiles != null)
            { 
              // 设置照片
              Picture = Image.FromFile(DropFiles[0]);
            }  
        }

        private void FingerPictureBox_DragEnter(object sender, DragEventArgs e)
        {   // 拖放时显示的效果
            e.Effect = DragDropEffects.Link;
        }

        private void FingerPictureBox_BackgroundImageChanged(object sender, EventArgs e)
        {
           
        }

        private void changeFrameImage()
        {
          if (_FrameImage == null)
            return;

          this.pictureBox1.Size = _FrameImage.Size;
          this.Size = _FrameImage.Size;

          generateBackground();
        }
        private void changePicture()
        {
          if (_Picture == null)
            return;

          generateBackground();
        }

        private void changePictureRegion()
        {
        }

        private void generateBackground()
        {
          /// 合成图片
          Bitmap bitmap = new Bitmap(this.Size.Width, this.Size.Height, PixelFormat.Format32bppArgb);
          Graphics gra = Graphics.FromImage(bitmap);
          gra.Clear(Color.Transparent);
          
          /// 把Picture压缩至合适的大小, 绘制照片
          if(Picture != null)
          {
            /// 防止图片区域错误
            if (_PictureRegion.Width == 0 || _PictureRegion.Height == 0)
            {
              _PictureRegion = new Rectangle(0, 0, this.Size.Width, this.Size.Height);
            }

            Image pic = GetReducedImage(Picture, _PictureRegion.Width, _PictureRegion.Height);
            gra.DrawImage(pic, _PictureRegion);
          }
          /// 再绘制外框
          if(FrameImage != null)
          {
            gra.DrawImage(FrameImage, new Rectangle(0, 0, this.Size.Width, this.Size.Height));
          }

          ActiveImage = bitmap;
        }

        /// <summary>
        /// 生成缩略图重载方法1，返回缩略图的Image对象
        /// </summary>
        /// <param name="resource">原图</param>
        /// <param name="width">缩略图的宽度</param>
        /// <param name="height">缩略图的高度</param>
        /// <returns>缩略图的Image对象</returns>
        public Image GetReducedImage(Image resource, int width, int height)
        {
          //用指定的大小和格式初始化Bitmap类的新实例
          Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
          //从指定的Image对象创建新Graphics对象
          Graphics graphics = Graphics.FromImage(bitmap);
          //清除整个绘图面并以透明背景色填充
          graphics.Clear(Color.Transparent);
          //在指定位置并且按指定大小绘制原图片对象
          graphics.DrawImage(resource, new Rectangle(0, 0, width, height));
          return bitmap;
        }

        /// <summary>
        /// 控件上图像的布局
        /// </summary>
        public ImageLayout ImageLayout
        {
            get
            {
                return this.Controls[0].BackgroundImageLayout;
            }
            set
            {
                this.Controls[0].BackgroundImageLayout = value;
            }
        }

        /// <summary>
        /// 控件大小
        /// </summary>
        public Size ImageSize
        {
            get
            {
                return this.Size;
            }
            set
            {
                this.Controls[0].Size = this.Size = value;
            }
        }

        /// <summary>
        /// 初始图像
        /// </summary>
        public Image InitialImage
        {
            get
            {
                return _InitialImage;
            }
            set
            {
                this.Controls[0].BackgroundImage = _InitialImage = value;
            }
        }

        /// <summary>
        /// 活动图像
        /// </summary>
        public Image ActiveImage
        {
            get
            {
                return this.Controls[0].BackgroundImage;
            }
            set
            {
                this.Controls[0].BackgroundImage = value;
            }
        }

        /// <summary>
        /// 外框
        /// </summary>
        public Image FrameImage
        {
          get
          {
            return _FrameImage;
          }
          set
          {
            _FrameImage = value;
            changeFrameImage();
          }
        }

        /// <summary>
        /// 相片
        /// </summary>
        public Image Picture
        {
          get
          {
            return _Picture;
          }
          set
          {
            _Picture = value;
            changePicture();
          }
        }
        /// <summary>
        /// 相片区域
        /// </summary>
        public Rectangle PictureRegion
        {
          get
          {
            return _PictureRegion;
          }
          set
          {
            _PictureRegion = value;
            changePictureRegion();
          }
        }
    }
}
